home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / shlex.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  224 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import os.path as os
  5. import sys
  6. from collections import deque
  7.  
  8. try:
  9.     from cStringIO import StringIO
  10. except ImportError:
  11.     from StringIO import StringIO
  12.  
  13. __all__ = [
  14.     'shlex',
  15.     'split']
  16.  
  17. class shlex:
  18.     
  19.     def __init__(self, instream = None, infile = None, posix = False):
  20.         if isinstance(instream, basestring):
  21.             instream = StringIO(instream)
  22.         
  23.         if instream is not None:
  24.             self.instream = instream
  25.             self.infile = infile
  26.         else:
  27.             self.instream = sys.stdin
  28.             self.infile = None
  29.         self.posix = posix
  30.         if posix:
  31.             self.eof = None
  32.         else:
  33.             self.eof = ''
  34.         self.commenters = '#'
  35.         self.wordchars = 'abcdfeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  36.         if self.posix:
  37.             self.wordchars += '\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde'
  38.         
  39.         self.whitespace = ' \t\r\n'
  40.         self.whitespace_split = False
  41.         self.quotes = '\'"'
  42.         self.escape = '\\'
  43.         self.escapedquotes = '"'
  44.         self.state = ' '
  45.         self.pushback = deque()
  46.         self.lineno = 1
  47.         self.debug = 0
  48.         self.token = ''
  49.         self.filestack = deque()
  50.         self.source = None
  51.         if self.debug:
  52.             print 'shlex: reading from %s, line %d' % (self.instream, self.lineno)
  53.         
  54.  
  55.     
  56.     def push_token(self, tok):
  57.         if self.debug >= 1:
  58.             print 'shlex: pushing token ' + repr(tok)
  59.         
  60.         self.pushback.appendleft(tok)
  61.  
  62.     
  63.     def push_source(self, newstream, newfile = None):
  64.         if isinstance(newstream, basestring):
  65.             newstream = StringIO(newstream)
  66.         
  67.         self.filestack.appendleft((self.infile, self.instream, self.lineno))
  68.         self.infile = newfile
  69.         self.instream = newstream
  70.         self.lineno = 1
  71.         if self.debug:
  72.             if newfile is not None:
  73.                 print 'shlex: pushing to file %s' % (self.infile,)
  74.             else:
  75.                 print 'shlex: pushing to stream %s' % (self.instream,)
  76.         
  77.  
  78.     
  79.     def pop_source(self):
  80.         self.instream.close()
  81.         (self.infile, self.instream, self.lineno) = self.filestack.popleft()
  82.         if self.debug:
  83.             print 'shlex: popping to %s, line %d' % (self.instream, self.lineno)
  84.         
  85.         self.state = ' '
  86.  
  87.     
  88.     def get_token(self):
  89.         if self.pushback:
  90.             tok = self.pushback.popleft()
  91.             if self.debug >= 1:
  92.                 print 'shlex: popping token ' + repr(tok)
  93.             
  94.             return tok
  95.         
  96.         raw = self.read_token()
  97.         if self.source is not None:
  98.             while raw == self.source:
  99.                 spec = self.sourcehook(self.read_token())
  100.                 if spec:
  101.                     (newfile, newstream) = spec
  102.                     self.push_source(newstream, newfile)
  103.                 
  104.                 raw = self.get_token()
  105.         
  106.         while raw == self.eof:
  107.             if not self.filestack:
  108.                 return self.eof
  109.                 continue
  110.             self.pop_source()
  111.             raw = self.get_token()
  112.         if self.debug >= 1:
  113.             if raw != self.eof:
  114.                 print 'shlex: token=' + repr(raw)
  115.             else:
  116.                 print 'shlex: token=EOF'
  117.         
  118.         return raw
  119.  
  120.     
  121.     def read_token(self):
  122.         quoted = False
  123.         escapedstate = ' '
  124.         while True:
  125.             nextchar = self.instream.read(1)
  126.             if nextchar == '\n':
  127.                 self.lineno = self.lineno + 1
  128.             
  129.             if self.debug >= 3:
  130.                 print 'shlex: in state', repr(self.state), 'I see character:', repr(nextchar)
  131.             
  132.             None if self.state is None else nextchar in self.escape
  133.             if self.state in self.quotes:
  134.                 quoted = True
  135.                 if not nextchar:
  136.                     if self.debug >= 2:
  137.                         print 'shlex: I see EOF in quotes state'
  138.                     
  139.                     raise ValueError, 'No closing quotation'
  140.                 
  141.                 if nextchar == self.state:
  142.                     if not self.posix:
  143.                         self.token = self.token + nextchar
  144.                         self.state = ' '
  145.                         break
  146.                     else:
  147.                         self.state = 'a'
  148.                 elif self.posix and nextchar in self.escape and self.state in self.escapedquotes:
  149.                     escapedstate = self.state
  150.                     self.state = nextchar
  151.                 else:
  152.                     self.token = self.token + nextchar
  153.             self.state in self.escapedquotes
  154.             None if self.state in self.escape else self.whitespace_split
  155.         result = self.token
  156.         self.token = ''
  157.         if self.posix and not quoted and result == '':
  158.             result = None
  159.         
  160.         if self.debug > 1:
  161.             if result:
  162.                 print 'shlex: raw token=' + repr(result)
  163.             else:
  164.                 print 'shlex: raw token=EOF'
  165.         
  166.         return result
  167.  
  168.     
  169.     def sourcehook(self, newfile):
  170.         if newfile[0] == '"':
  171.             newfile = newfile[1:-1]
  172.         
  173.         if isinstance(self.infile, basestring) and not os.path.isabs(newfile):
  174.             newfile = os.path.join(os.path.dirname(self.infile), newfile)
  175.         
  176.         return (newfile, open(newfile, 'r'))
  177.  
  178.     
  179.     def error_leader(self, infile = None, lineno = None):
  180.         if infile is None:
  181.             infile = self.infile
  182.         
  183.         if lineno is None:
  184.             lineno = self.lineno
  185.         
  186.         return '"%s", line %d: ' % (infile, lineno)
  187.  
  188.     
  189.     def __iter__(self):
  190.         return self
  191.  
  192.     
  193.     def next(self):
  194.         token = self.get_token()
  195.         if token == self.eof:
  196.             raise StopIteration
  197.         
  198.         return token
  199.  
  200.  
  201.  
  202. def split(s, comments = False):
  203.     lex = shlex(s, posix = True)
  204.     lex.whitespace_split = True
  205.     if not comments:
  206.         lex.commenters = ''
  207.     
  208.     return list(lex)
  209.  
  210. if __name__ == '__main__':
  211.     if len(sys.argv) == 1:
  212.         lexer = shlex()
  213.     else:
  214.         file = sys.argv[1]
  215.         lexer = shlex(open(file), file)
  216.     while None:
  217.         tt = lexer.get_token()
  218.         if tt:
  219.             print 'Token: ' + repr(tt)
  220.             continue
  221.         break
  222.         continue
  223. __name__ == '__main__'
  224.